package com.yjc.hire.controller.admin;

import com.yjc.bo.QueryCompanyBO;
import com.yjc.bo.ReviewCompanyBO;
import com.yjc.hire.VO.CompanyInfoVO;
import com.yjc.hire.base.BaseInfoProperties;
import com.yjc.hire.enums.CompanyReviewStatus;
import com.yjc.hire.feign.UserMicroServiceFeign;
import com.yjc.hire.grace.result.GraceJSONResult;
import com.yjc.hire.grace.result.PageGridResult;
import com.yjc.hire.service.CompanyService;
import com.yjc.pojo.Company;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.validation.Valid;
import java.util.List;
import java.util.Objects;

/**
 * </p>
 *
 * @author jcyin
 * @since 2025/8/12
 * </p>
 */

@RestController
@RequestMapping("company/admin")
public class AdminCompanyController extends BaseInfoProperties {
    @Autowired
    private CompanyService companyService;

    @Autowired
    private UserMicroServiceFeign userMicroServiceFeign;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @PostMapping("getCompanyList")
    public GraceJSONResult adminGetCompanyList(@RequestBody @Valid  QueryCompanyBO query,
                                               Integer page,Integer limit){
        if (page == null) page = 1;
        if (limit == null) limit = 10;
        PageGridResult<List<CompanyInfoVO>> companyPageList = companyService.queryCompanyListPaged(query,page,limit);
        return GraceJSONResult.ok(companyPageList);
    }

    @PostMapping("getCompanyInfo")
    public GraceJSONResult adminGetCompanyInfo(String companyId){
        CompanyInfoVO company = companyService.adminGetCompanyInfo(companyId);
        return GraceJSONResult.ok(company);
    }

    @PostMapping("doReview")
    public GraceJSONResult adminDoReview(@RequestBody ReviewCompanyBO reviewCompanyBO){
        companyService.updateCompanyInfo(reviewCompanyBO);
        if (Objects.equals(reviewCompanyBO.getReviewStatus(), CompanyReviewStatus.SUCCESSFUL.type)){
            userMicroServiceFeign.changeUserToHr(reviewCompanyBO.getHrUserId());
        }
        //清楚用户端企业缓存
        stringRedisTemplate.delete(REDIS_COMPANY_BASE_INFO + ":" + reviewCompanyBO.getCompanyId());
        return GraceJSONResult.ok();
    }


}
